﻿#region - Class details -
/*  
 *  Developer   : Wicus Botha
 *  Description : 
 *  History     : WB - 2009-12-0 - Created
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace IQ.Base
{
    public static class StringExtensions
    {
        public static byte ToByteDefault(this String str, byte defValue)
        {
            try
            {
                return Convert.ToByte(str);
            }
            catch
            {
                return defValue;
            }
        }

        public static int ToIntDefault(this String str, int defValue)
        {
            try
            {
                return Convert.ToInt32(str);
            }
            catch
            {
                return defValue;
            }
        }

        public static decimal ToDecimalDefault(this String str, decimal defValue)
        {
            try
            {
                return Convert.ToDecimal(str);
            }
            catch
            {
                return defValue;
            }
        }

        public static short ToShortDefault(this String str, short defValue)
        {
            try
            {
                return Convert.ToInt16(str);
            }
            catch
            {
                return defValue;
            }
        }

        public static string Left(this String str, int length)
        {
            if (length < str.Length)
                return str.Substring(0, length);
            else
                return str;
        }
        public static string Right(this String str, int length)
        {
            if (length < str.Length)
                return str.Substring(str.Length - length, length);
            else
                return str;
        }

        public static string Stuff(this string str, int start, int length, string replaceWith_expression)
        {
            return str.Remove(start, length).Insert(start, replaceWith_expression);
        }

        public static string ToPascalCase(this String str)
        {
            //msdn.microsoft.com/en-us/library/x2dbyw72(v=vs.71).aspx		
            if (!String.IsNullOrEmpty(str))
                return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str);
            return "";
        }
        public static string TrimToPascalCase(this String str)
        {
            if (!String.IsNullOrEmpty(str))
                return ToPascalCase(str).Trim();
            return "";
        }
    }

    public static class CharExtensions
    {
        public static int ToIntDefault(this Char c, int defValue)
        {
            if (Char.IsNumber(c))
            {
                return (int)(c - '0');
            }
            else
            {
                return defValue;
            }
        }
    }

    public static class NumberExtensions
    {
        public static bool Between(this byte val, double from, double to)
        {
            return val >= from && val <= to;
        }
        public static bool Between(this short val, double from, double to)
        {
            return val >= from && val <= to;
        }
        public static bool Between(this int val, double from, double to)
        {
            return val >= from && val <= to;
        }
        public static bool Between(this long val, double from, double to)
        {
            return val >= from && val <= to;
        }
        public static bool Between(this float val, double from, double to)
        {
            return val >= from && val <= to;
        }
        public static bool Between(this double val, double from, double to)
        {
            return val >= from && val <= to;
        }
        public static bool Between(this decimal val, decimal from, decimal to)
        {
            return val >= from && val <= to;
        }
        public static bool Between(this DateTime val, DateTime from,DateTime to)
        {
            return val >= from && val <= to;
        }
        public static bool Between(this Enum val, Enum from, Enum to)
        {
            return val.CompareTo(from) >= 0 && val.CompareTo(to) <= 0;
        }
        public static bool In(this byte val, params double [] values)
        {
            return values.Any(itm => itm == val);
        }
        public static bool In(this short val, params double[] values)
        {
            return values.Any(itm => itm == val);
        }
        public static bool In(this int val, params double[] values)
        {
            return values.Any(itm => itm == val);
        }
        public static bool In(this long val, params double[] values)
        {
            return values.Any(itm => itm == val);
        }
        public static bool In(this float val, params double[] values)
        {
            return values.Any(itm => itm == val);
        }
        public static bool In(this double val, params double[] values)
        {
            return values.Any(itm => itm == val);
        }
        public static bool In(this decimal val, params decimal[] values)
        {
            return values.Any(itm => itm == val);
        }
        public static bool In(this int val, IEnumerable<int> values)
        {
            return values.Any(itm => itm == val);
        }
        public static bool In<T>(this T val, params T[] values)
        {
            return values.Any(itm => itm.Equals(val));
        }

    }

}
